<template>
  <div class="maintenanceorder-page">
    <el-card class="search" shadow="never">
      <el-form :inline="true" :model="formData" class="form-inline">
        <el-form-item label="工单编号:">
          <el-input
            v-model="formData.taskCode"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="工单状态:">
          <el-select v-model="formData.status" placeholder="请选择" clearable>
            <el-option
              :label="item.statusName"
              :value="item.statusId"
              v-for="item in allTaskStatus"
              :key="item.statusId"
            ></el-option>
            <i slot="suffix" class="el-input__icon el-icon-date"></i>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onQuery"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never" class="result">
      <!-- 新建 工单配置 -->
      <el-row class="operation">
        <el-button
          type="danger"
          class="el-button--second"
          icon="el-icon-circle-plus-outline"
          @click="addFn"
          >新建</el-button
        >
      </el-row>
      <!-- 表格区域 -->
      <el-table
        :data="tableData"
        highlight-current-row
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column property="taskCode" label="工单编号" width="150">
        </el-table-column>
        <el-table-column property="innerCode" label=" 设备编号" width="135">
        </el-table-column>
        <el-table-column
          property="taskType.typeName"
          label="工单类型"
          width="135"
        >
        </el-table-column>
        <el-table-column
          property="createType"
          :formatter="formatterCreateType"
          label="工单方式"
          width="100"
        >
        </el-table-column>
        <el-table-column
          property="taskStatusTypeEntity.statusName"
          label="工单状态"
          width="100"
        >
        </el-table-column>
        <el-table-column property="userName" label="运营人员" width="135">
        </el-table-column>
        <el-table-column
          property="createTime"
          :formatter="formatterCreateTime"
          label="创建日期"
          width="180"
        >
        </el-table-column>
        <el-table-column label="操作" width="135">
          <template v-slot="{ row }">
            <el-button type="text" size="small" @click="clickDetailFn(row)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        v-if="page.totalCount > 10"
        prev-text="上一页"
        next-text="下一页"
        @current-change="handleCurrentChange"
        :current-page.sync="page.pageIndex"
        :page-size="page.pageSize"
        layout="slot,prev, next"
      >
        <template>
          <span class="el-pagination__total"
            >共{{ page.totalCount }}条记录&nbsp;&nbsp;第&nbsp;{{
              page.pageIndex
            }}/{{ page.totalPage }}页</span
          >
        </template>
      </el-pagination>
    </el-card>
    <!-- 新增工单 -->
    <el-dialog
      title="新增工单"
      :visible.sync="addVisible"
      width="630px"
      :before-close="addClose"
      class="addDialog"
    >
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addForm"
        label-width="140px"
        class="addForm"
      >
        <el-form-item label="设备编号:" prop="innerCode">
          <el-input
            v-model="addForm.innerCode"
            placeholder="请输入"
            maxlength="15"
            show-word-limit
            @change="getOperatorList"
          ></el-input>
        </el-form-item>
        <el-form-item label="工单类型:" prop="productType">
          <el-select v-model="addForm.productType" placeholder="请选择">
            <el-option
              v-for="item in replenishTaskType"
              :key="item.typeId"
              :label="item.typeName"
              :value="item.typeId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="运维人员:" prop="assignorId">
          <el-select v-model="addForm.assignorId" placeholder="请选择">
            <el-option
              v-for="item in operatorList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注:" prop="desc">
          <el-input
            type="textarea"
            v-model="addForm.desc"
            placeholder="请输入备注(不超过40个字)"
            maxlength="15"
            show-word-limit
            :rows="4"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addClose" type="primary" class="el-button--first"
          >取 消</el-button
        >
        <el-button type="danger" class="el-button--second" @click="addSubmit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 查看详情弹框 -->
    <el-dialog
      title="工单详情"
      :visible.sync="detailVisible"
      width="630px"
      :before-close="detailClose"
      class="detailDialog"
    >
      <template>
        <div class="task-status" v-if="detailType === 4">
          <img class="icon" src="@/assets/lkd/done1.png" alt="" />
          <span class="status">完成</span>
          <img class="pic" src="@/assets/lkd/done2.png" alt="" />
        </div>
        <div class="task-status" v-else-if="detailType === 3">
          <img class="icon" src="@/assets/lkd/cancel1.png" alt="" />
          <span class="status">取消</span>
          <img class="pic" src="@/assets/lkd/cancel2.png" alt="" />
        </div>
        <div class="task-status" v-else>
          <img class="icon" src="@/assets/lkd/undetermined1.png" alt="" />
          <span class="status">待办</span>
          <img class="pic" src="@/assets/lkd/undetermined2.png" alt="" />
        </div>
        <el-form label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="设备编号:">
                {{ detailRow.innerCode }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="创建日期:">
                {{ detailRow.createTime }}
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="detailType === 4">
              <el-form-item label="完成日期:">
                {{ detailRow.updateTime }}
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="detailType === 3">
              <el-form-item label="取消日期:">
                {{ detailRow.updateTime }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="运维人员:">
                {{ detailRow.userName }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工单类型:">
                {{ detailRow.taskType?.typeName }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工单方式:">
                {{ detailRow.createType ? "手动" : "自动" }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注:">
                {{ detailRow.desc }}
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </template>
      <span slot="footer" class="dialog-footer" v-if="detailType !== 4">
        <el-button
          @click="cancelOrDoneDialog"
          type="primary"
          class="el-button--first"
          >{{ detailType === 3 ? "重新创建" : "取消工单" }}</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { getallTaskStatus, getTaskSearch, getTaskType, getOperatorList, taskCreate, getTaskInfo, TaskCancel } from '@/api/order'
export default {
  created () {
    this.getallTaskStatus()
    this.getTaskSearch()
    this.getTaskType()
  },
  data () {
    return {
      formData: {
        taskCode: '',
        status: ''
      },
      allTaskStatus: [],
      taskType: [],
      page: {
        pageIndex: 1,
        pageSize: 10,
        isRepair: true,
        totalCount: 0,
        totalPage: 0
      },
      tableData: [],
      loading: false,
      addVisible: false,
      addForm: {
        assignorId: '',
        createType: 1,
        innerCode: '',
        userId: 1,
        productType: '',
        desc: '',
        details: []
      },
      operatorList: [],
      detailType: '',
      detailVisible: false,
      detailRow: {},
      addRules: {
        innerCode: [{ required: true, message: '不能为空', trigger: 'blur' }],
        productType: [{ required: true, message: '不能为空', trigger: 'blur' }],
        userName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        desc: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async getTaskType () {
      this.taskType = await getTaskType()
    },
    async getallTaskStatus () {
      this.allTaskStatus = await getallTaskStatus()
    },
    async getTaskSearch () {
      try {
        this.loading = true
        const res = await getTaskSearch({ ...this.page, ...this.formData })
        // console.log(res)
        this.tableData = res.currentPageRecords
        this.page.totalCount = Number(res.totalCount)
        this.page.totalPage = Number(res.totalPage)
      } catch (error) {
        console.log(error)
      } finally {
        this.loading = false
      }
    },
    formatterCreateType (row, column, cellValue, index) {
      if (cellValue === 0) return '自动'
      if (cellValue === 1) return '手动'
    },
    formatterCreateTime (row, column, cellValue, index) {
      return dayjs(cellValue).format('YYYY.MM.DD HH:mm:ss')
    },
    handleCurrentChange () {
      this.getTaskSearch()
    },
    onQuery () {
      this.getTaskSearch()
    },
    addFn () {
      this.addVisible = true
    },
    addClose () {
      this.addForm = {
        innerCode: '',
        productType: '',
        userId: '',
        desc: ''
      }
      this.$refs.addForm.resetFields()
      this.addVisible = false
    },
    async getOperatorList () {
      this.operatorList = await getOperatorList(this.addForm.innerCode)
    },
    async addSubmit () {
      try {
        await taskCreate(this.addForm)
        this.addClose()
        this.getTaskSearch()
      } catch (error) {
        console.log(error)
      }
    },
    clickDetailFn (row) {
      this.detailVisible = true
      this.detailRow = row
      this.detailRow.createTime = dayjs(row.createTime).format('YYYY.MM.DD HH:mm:ss')
      this.detailRow.updateTime = dayjs(row.updateTime).format('YYYY.MM.DD HH:mm:ss')
      // 1待办是2是进行3是取消4是完成
      this.detailType = this.detailRow.taskStatus
    },
    detailClose () {
      this.detailVisible = false
    },
    async cancelOrDoneDialog () {
      // 如果是取消工单，获取工单详情，弹出新建工单
      if (this.detailType === 3) {
        this.detailClose()
        this.addVisible = true
        const res = await getTaskInfo(this.detailRow.taskId)
        this.addForm.innerCode = res.innerCode
        this.getOperatorList(this.addForm.innerCode)
        // 下拉框v-model绑定的值与value的值一样就代表默认选中
        this.addForm.assignorId = res.assignorId
        this.addForm.productType = res.taskType.typeId
      } else {
        // 如果是代办状态，弹出取消确认框
        this.$confirm('取消工单后，将不能恢复，是否确认取消?', '取消工单', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          try {
            await TaskCancel(this.detailRow.taskId)
            this.getTaskSearch()
            this.detailClose()
          } catch (error) {
            console.log(error)
          }
        })
      }
    }
  },
  computed: {
    replenishTaskType () {
      return this.taskType.filter(item => item.typeId !== 2)
    }
  },
  watch: {},
  filters: {},
  components: {}
}
</script>

<style scoped lang='scss'>
.maintenanceorder-page {
  .result {
    .operation {
      margin-bottom: 20px;
    }
  }
  .el-pagination {
    display: flex;
    height: 96px;
    align-items: center;
    .el-pagination__total {
      flex: 1;
      font-size: 16px !important;
      color: #dbdfe5 !important;
    }
    ::v-deep button:disabled {
      color: #d8dde3;
      background: #edf0f9;
    }
    ::v-deep button {
      width: 70px;
      height: 32px;
      margin: 0 16px;
      border-radius: 2px;
      background-color: #d5ddf8;
    }
  }

  .configForm {
    ::v-deep .el-input {
      width: 396px;
      .el-input__prefix,
      .el-input__suffix {
        width: 40px;
        height: 19px;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #f8fafd;
        color: #606266;
        cursor: pointer;
        font-size: 13px;
        .el-button {
          padding: unset;
          width: 100%;
          height: 100%;
          background: unset;
          border: unset;
        }
      }
      .el-input__suffix {
        top: unset;
        bottom: 1px;
        right: 1px;
        border-right: none;
        border-left: 1px solid #d8dde3;
        border-radius: 0 0 4px 0;
        .el-input__suffix-inner {
          display: flex;
          align-items: center;
          width: 100%;
          height: 100%;
        }
      }
      .el-input__prefix {
        left: unset;
        top: 1px;
        right: 1px;
        border-radius: 0 4px 0 0;
        border-left: 1px solid #d8dde3;
        border-bottom: 1px solid #d8dde3;
      }
    }
  }
  .addForm {
    ::v-deep .el-form-item__content {
      width: 396px;
    }
  }
}
</style>
<style lang="scss">
.maintenanceorder-page {
  .el-card {
    border-radius: unset;
    border: unset;
  }
  .search.el-card {
    margin-bottom: 20px;
    height: 64px;
    display: flex;
    align-items: center;
    padding-left: 17px;
    background-color: #fff;
    .el-card__body {
      padding: unset;
      .el-form-item {
        margin: unset;
        margin-right: 10px;
      }
    }
  }
  .el-button {
    border: none !important;
    &.el-button--first {
      background-color: #fbf4f0 !important;
      color: #655b56 !important;

      &:hover {
        background-color: #f3e7e1 !important;
      }
    }
  }
}
</style>
